package com.eptok.sunftp;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import sun.net.TelnetInputStream;
import sun.net.TelnetOutputStream;
import sun.net.ftp.FtpClient;

/**
 * 
 * @author 	yuelong
 * @email 	yue_long@126.com
 * @date 	Jul 8, 2012 11:11:50 AM
 * @use		使用sun提供的ftp工具类进行ftp文件操作
 */
public class SunFtpOperator {

	private FtpClient ftpClient;
	
	/**
	 * 连接ftp服务器
	 * @param serverIP
	 * @param user
	 * @param passwrd
	 * @param path
	 */
	public void connectServer(String serverIP,String user,String password,String path) throws Exception{
		
		ftpClient = new FtpClient();
		ftpClient.openServer(serverIP);
		ftpClient.login(user, password);
		//path 主目录下的子目录
		if(path!=null){
			ftpClient.cd(path);
		}
		ftpClient.binary(); //使用2进制上传、下载文件
	}
	
	/**
	 * 断开与ftp服务器的连接
	 * @throws Exception
	 */
	public void disconnectServer() throws Exception{
		if(ftpClient!=null){
			ftpClient.closeServer();
		}
	}
	
	
	/**
	 * 上传文件
	 * @param localFile 本地文件路径
	 * @param remoteFileName 远程文件命名
	 * @return  0 上传成功 -1 上传失败
	 * @throws Exception
	 */
	public int upload(String localFile,String remoteFileName) throws Exception{
		int flag = -1;
		TelnetOutputStream tos = null;
		FileInputStream fis = null;
		try{
			File file_in = new File(localFile);
			//检查本地文件是否存在
			if(!file_in.exists()){
				return flag;
			}
			//打开输出流
			tos = ftpClient.put(remoteFileName);
			fis = new FileInputStream(file_in);
			byte[] buffer = new byte[1024];
			int c;
			while((c=fis.read(buffer))!=-1){
				tos.write(buffer,0,c);
			}
			tos.flush();
			flag = 0;
		}catch(Exception e){
			e.printStackTrace();
			throw e;
		}finally{
			if(tos!=null){
				tos.close();
			}
			if(fis!=null){
				fis.close();
			}
		}
		return flag;
	}
	
	/**
	 * 上传文件
	 * @param localFilePath 本地文件路径
	 * @return
	 * @throws Exception
	 */
	public int upload(String localFilePath) throws Exception{
		String remoteFileName;
		if(localFilePath.indexOf("/")>-1){
			remoteFileName = localFilePath.substring(localFilePath.lastIndexOf("/")+1);
		}else{
			remoteFileName = localFilePath;
		}
		return upload(localFilePath, remoteFileName);
	}
	
	/**
	 * 下载文件
	 * @param remoteFileName 
	 * @param lcoalFile
	 * @return 0 下载成功  -1下载失败
	 */
	public int download(String remoteFileName,String localFile) throws Exception{
		int flag = -1;
		TelnetInputStream tis = null;
		FileOutputStream fos = null;
		try{
			tis = ftpClient.get(remoteFileName);
			File file_out = new File(localFile);
			fos = new FileOutputStream(file_out);
			byte[] buffer = new byte[1024];
			int c;
			while((c=tis.read(buffer))!=-1){
				fos.write(buffer,0,c);
			}
		}catch (Exception e){
			e.printStackTrace();
			throw e;
		}finally{
			if(tis!=null){
				tis.close();
			}
			if(fos!=null){
				fos.close();
			}
		}
		return flag;
	}
	
	/**
	 * 获取指定目录的文件列表
	 * @param path ftp相对路径
	 * @param charset 字符集编码
	 * @return
	 * @throws Exception
	 */
	public List<String> getFileList(String path,String charset) throws Exception{
		BufferedReader reader = null;
		List<String> fileList = new ArrayList<String>();
		try{
			reader = new BufferedReader (new InputStreamReader(ftpClient.nameList(path),charset));
			String fileName;
			while((fileName=reader.readLine())!=null){
				fileList.add(fileName);
			}
		}catch (Exception e){
			
		}finally{
			if(reader!=null){
				reader.close();
			}
		}
		return fileList;
	}
	
	/**
	 * 删除文件
	 * @param fileName 要删除的文件名
	 * @return 0删除成功 -1删除失败
	 */
	public int delte(String fileName)throws Exception{
		int flag = -1;
		try{
			ftpClient.sendServer("DELE "+fileName+"\r\n");
			flag = 1;
		}catch (Exception e){
			e.printStackTrace();
			throw e;
		}
		
		return flag;
	}
	
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		SunFtpOperator ftp = new SunFtpOperator();
		try {
			ftp.connectServer("192.168.1.188", "yuel", "yuel", "yuel");
			ftp.delte("111.txt");
			ftp.disconnectServer();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
